/* IconImage.m created by epeyton on Fri 17-Sep-1999 */

#import "IconImage.h"
#import <ScreenSaver/ScreenSaver.h>

@implementation IconImage

- (id)initWithPath:(NSString *)imagePath inRect:(NSRect)rect parent:(id)passedParent
{
    self = [super init];

    parent = passedParent;
    
    // load the app icon from this place
    image = [[[NSWorkspace sharedWorkspace] iconForFile:imagePath] retain];

    fade = SSRandomFloatBetween (0.1, 0.5);
    blackFade = .2;
    [image setScalesWhenResized:YES];

    [image setSize:rect.size];
	
    blackImage = [[NSImage alloc] initWithSize:rect.size];
    [blackImage setBackgroundColor:[NSColor blackColor]];
    [blackImage lockFocus];
    PSsetgray(0.0);
    NSRectFill(NSMakeRect(0.0,0.0,[blackImage size].width,[blackImage size].height));
    [blackImage unlockFocus];
    
    // place it at point
    imagePoint = rect.origin;

    counter = 0;
    life = SSRandomFloatBetween(10.0, 20.0);
    
    return self;
}

- (void)display
{    
    if (fade < 1) {
        fade += .01;
    } else {
        counter++;
    }

    if (counter > life) {
        blackFade += .01;
        [blackImage dissolveToPoint:imagePoint fraction:blackFade];
        if (blackFade > 1) {
        	[parent removeIconImage:self];
        }
    } else if (fade < 1) {
        [image dissolveToPoint:imagePoint fraction:fade];
    }
}

- (void)dealloc
{
    [image release];
    [blackImage release];
    [super dealloc];
    return;
}

- (NSRect)imageRect
{
    return(NSMakeRect(imagePoint.x,imagePoint.y,[blackImage size].width,[blackImage size].height));
}

@end
